package logic

import (
	"context"
	"errors"
	"good/goodrpc/goodrpc"
	"good/goodrpc/internal/svc"
	"good/models/mysql"

	"github.com/zeromicro/go-zero/core/logx"
)

type LikeAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLikeAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LikeAddLogic {
	return &LikeAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *LikeAddLogic) LikeAdd(in *goodrpc.LikeAddRequest) (*goodrpc.LikeAddResponse, error) {
	// todo: add your logic here and delete this line
	var user mysql.User

	mysql.DB.Debug().Where("id=?", in.UserId).Find(&user)
	if user.ID == 0 {
		return nil, errors.New("用户不存在")
	}
	like := mysql.Like{
		UserId: int(in.UserId),
		GoodId: int(in.GoodId),
	}
	mysql.DB.Debug().Create(&like)

	return &goodrpc.LikeAddResponse{
		LikeId: int64(like.ID),
	}, nil
}
